package com.greendragon.util; 

import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
/** 
 * @author wulibin, exceptsky@gmail.com, 2011-9-11 下午12:09:34
 * 
 */
public class CookieUtil {

	public static void addCookie(HttpServletResponse response, String key, String value, int seconds) {
		Cookie cookie = new Cookie(key, value);
		cookie.setMaxAge(seconds);
		response.addCookie(cookie);
	}

	public static String getCookie(HttpServletRequest request, String key) {
		Cookie[] cookies = request.getCookies();
		if(cookies == null){
			return null;
		}
		for (Cookie cookie : cookies) {
			if (cookie.getName().equals(key)) {
				return cookie.getValue();
			}
		}
		return null;
	}

	public static void removeCookie(HttpServletResponse response, String key) {
		CookieUtil.addCookie(response,key,null,0);
	}
}
 